/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package drt;

import evaluation.Edge;
import evaluation.Graph;
import evaluation.Vertex;
import java.util.LinkedList;
import java.util.Map;

/**
 * timex(t, <sym>)
 * @author phong
 */
public class DRSPredTimexCondition extends DRSPredCondition {
    
    public DRSPredTimexCondition(String symbol,  DrtEntity arg) {
        this.symbol = symbol;
        this.args = new LinkedList<DrtEntity>();
        args.add(arg);
    }
    
    @Override
    public String toString() {
        return "timex(" + this.args.get(0) + "," + this.symbol + ")";
    }
    
    @Override
    public int hashCode() {
        return this.symbol.hashCode() * 17;
    }
    
    @Override
    public boolean equals(Object other) {
        if (other instanceof DRSPredTimexCondition) {
            DRSPredTimexCondition that = (DRSPredTimexCondition)other;
            return that.args.equals(this.args) && 
                    that.symbol.equals(this.symbol);
        }
        return false;
    }

    @Override
    public DRSPredTimexCondition replace(DrtVariable var, DrtExpression exp) {
        if (this.args.get(0).equals(var)) {
            if (exp instanceof DrtEntityVariable)
                return new DRSPredTimexCondition(symbol, (DrtEntity)exp);
            else if (exp instanceof DrtVariable)
                return new DRSPredTimexCondition(symbol, new DrtEntityVariable(((DrtVariable)exp).getName()));
            else 
                throw new Error("incorrect type");
        }
        else 
            return this;
    }

    @Override
    public Graph toGraph() {
        Graph g = new Graph();
        
        Vertex v = new Vertex(this.args.get(0).getName(), Vertex.TYPE_REF);
        Vertex f = new Vertex("<timex> " + this.symbol, Vertex.TYPE_PRED);
        g.addEdge(new Edge("", v, f));
        g.addMarks(f);
        
        return g;
    }
    
    @Override
    public DRSPredTimexCondition getTemplate(Map<String,String> map) {
        String symtemp = map.get(this.symbol);
        if (symtemp == null) {
            symtemp = TemplateNaming.getUniqueName();
            map.put(symbol, symtemp);
        }
        return new DRSPredTimexCondition(symtemp, this.args.get(0));
    }
}
